package 力扣.数组;

import java.util.ArrayList;
import java.util.List;

public class 较大分组的位置830 {
    public static void main(String[] args) {
//        String s = "abc";
//        String s = "abcdddeeeeaabbbcd";
//        String s = "abbxxxxzzy";
//        String s="a";
        String s = "baaaaa";
        List<List<Integer>> lists = largeGroupPositions(s);
        System.out.println(lists);
    }
    public static List<List<Integer>> largeGroupPositions(String s) {
        int length = s.length();
        char c = '0';//保证了第一个元素被比较
        int count=1;
        int start=-1;
        int end=0;
        ArrayList<List<Integer>> list = new ArrayList<>();
        for (int i = 0; i < length; i++){
            char cc = s.charAt(i);
             if (cc == c){
                 count++;
             }else{
                 //当cc!=c时，才计算count ，保证了count大于3的情况
                 if( count>=3 ){
                     ArrayList<Integer> listNode = new ArrayList<>();
                     start = i - count;
                     end = i-1;
                     listNode.add(start);
                     listNode.add(end);
                     list.add(listNode);
                     count = 1;
                 }else{
                     //不等于3，则舍弃
                     count=1;
                 }

             }
             c = cc;

        }
        //保证了  s = "baaaaa";  即相同字符相同结尾的情况
        if (count >= 3){
            ArrayList<Integer> listNode = new ArrayList<>();
            start = length - count;
            end = length-1;
            listNode.add(start);
            listNode.add(end);
            list.add(listNode);
        }
        return list;
    }
}
